<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

//use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use app\admin\model\ProductCateModel;
use think\Db;
use app\admin\model\ThemeModel;


class ProductCateController extends AdminBaseController
{
    /**
     * 产品分类列表
     */
    public function index()
    {
        $ProductCateModel = new ProductCateModel();
        $cates        = $ProductCateModel->adminProductCateList();

        $this->assign('cates', $cates);
        return $this->fetch();
    }

    /**
     * 添加文章分类
     * @adminMenu(
     *     'name'   => '添加文章分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加文章分类',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        
        return $this->fetch();
    }

    /**
     * 添加文章分类提交
     * @adminMenu(
     *     'name'   => '添加文章分类提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加文章分类提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $ProductCateModel = new ProductCateModel();

        $data = $this->request->param();

        $result = $this->validate($data, 'ProductCate');

        if ($result !== true) {
            $this->error($result);
        }

        $result = $ProductCateModel->addCate($data);

        if ($result === false) {
            $this->error('添加失败!');
        }

        $this->success('添加成功!', url('index'));

    }

    /**
     * 编辑文章分类
     * @adminMenu(
     *     'name'   => '编辑文章分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章分类',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
             $cate = ProductCateModel::get($id)->toArray();

             $this->assign('cate',$cate);

            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }

    }

    /**
     * 编辑文章分类提交
     * @adminMenu(
     *     'name'   => '编辑文章分类提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章分类提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data = $this->request->param();

        $result = $this->validate($data, 'ProductCate');

        if ($result !== true) {
            $this->error($result);
        }

        $ProductCateModel = new ProductCateModel();

        $result = $ProductCateModel->editCate($data);

        if ($result === false) {
            $this->error('保存失败!');
        }

        $this->success('保存成功!');
    }

   

    /**
     * 删除文章分类
     * @adminMenu(
     *     'name'   => '删除文章分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除文章分类',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $ProductCateModel = new ProductCateModel();
        $id                  = $this->request->param('id');
        //获取删除的内容
        $findCate = $ProductCateModel->where('id', $id)->find();

        if (empty($findCate)) {
            $this->error('分类不存在!');
        }

        $result = $ProductCateModel
            ->where('id', $id)
            ->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }

}
